#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>
#include <qtimer.h>
#include "torus.h"
#include "Cube.h"
#include "SimpleCursor.h"
#include <qcolordialog.h>
#include <qsharedpointer.h>
#include "BezierCurve.h"
#include "C2Curve.h"
#include "C0Curve.h"

class QtLogo;
class Primitive;

namespace CAD_CAM
{

	class GLWidget : public QGLWidget
	{
		Q_OBJECT

	public:
		GLWidget(const QGLFormat & format);
		~GLWidget();

		QSize minimumSizeHint() const;
		QSize sizeHint() const;

		bool getStereo() { return stereo; };
		QColor& getRightColor() { return rightColor; };
		QColor& getLeftColor() { return leftColor; };
		float getRplane() { return rPlane; };
	
		void paintEvent(QPaintEvent *event);
		void showEvent(QShowEvent *event);
		void drawCursorInfo(QPainter *painter);

	public slots:
		void setXRotation(int angle);
		void setYRotation(int angle);
		void setZRotation(int angle);
		void setRPlane(int R);
		void timeout( );
		void setWidth(double width);
		void setHeight(double height);
		void setLength(double length);
		void setTorusVerticesNumber(int verticesNumber);
		void setTorusStripesNumber(int stripesNumber);
		void setStereo(bool value) { stereo = value; update(); };
		void setLeftColor(QColor color) { leftColor = color; update(); };
		void setRightColor(QColor color) { rightColor = color; update(); };
		void setEyesWidth(double width) { eyesWidth = width; update(); };

	signals:
		void xRotationChanged(int angle);
		void yRotationChanged(int angle);
		void zRotationChanged(int angle);
	

	protected:
		void initializeGL();
		//void paintGL();
		void resizeGL(int width, int height);
		void mousePressEvent(QMouseEvent *event);
		void mouseMoveEvent(QMouseEvent *event);
		void keyPressEvent ( QKeyEvent * event );
		void wheelEvent ( QWheelEvent * event );
		bool prepareShaders();
		bool initShader(QGLShaderProgram& shader, const QString& fragFile, const QString& vertFile, const QString& geomFile);
		void draw();
		void drawStereo();
		void drawScene(QGLShaderProgram & shaderProgram);

		void handleCursorInput(QKeyEvent * event);

	private:
		QPoint lastPos;
	
		// scene elements
		QVector<QSharedPointer<Primitive>> sceneElements;
		QSharedPointer<Torus> torus;
		Curves::C0Curve b;

		SimpleCursor *cursor;
		QGLShaderProgram mShaderProgram;
		QGLShaderProgram bezierShaderProgram;

		QGLShaderProgram c0BezierShaderProgram;

		QGLShaderProgram stereoShaderProgram;
		QMatrix4x4 mViewMatrix;
		QMatrix4x4 mProjectionMatrix;
		QVector3D eyePosition;
		float rPlane;

		QSharedPointer<Primitive> selectedElement;
		QSharedPointer<Primitive> grabbedElement;

		const float delta;
		float cursorStep;

		bool isSelected;
		int selectedIndex;

		bool stereo;
		float eyesWidth;
		QColor leftColor, rightColor;
	};
}
#endif
